﻿
    using System;
    using System.Collections.Generic;
    using System.Windows;
    using System.Windows.Media;
    namespace WCYCReader.Main
{
    public class GetVisualChild
    {
        public static List<T> GetVisualChildCollection<T>(object parent) where T: UIElement
        {
            List<T> visualCollection = new List<T>();
            GetVisualChildCollection<T>(parent as DependencyObject, visualCollection);
            return visualCollection;
        }

        private static void GetVisualChildCollection<T>(DependencyObject parent, List<T> visualCollection) where T: UIElement
        {
            int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < childrenCount; i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(parent, i);
                if (child is T)
                {
                    visualCollection.Add(child as T);
                }
                else if (child != null)
                {
                    GetVisualChildCollection<T>(child, visualCollection);
                }
            }
        }
    }
}

